<script setup>
import Edit from './components/Edit.vue'
import {onMounted, ref} from "vue";
import axios from 'axios';

// TODO: 列表渲染
const list = ref([])

const getList = async () => {
  const res = await axios.get('/list');
  console.log(res)
  list.value = res.data
}

onMounted(() => getList())

// TODO: 删除功能
const onDelete = async (id) => {
  const res = await axios.delete(`/del/${id}`)
  getList()
}

// TODO: 编辑功能
const editRef = ref(null)
const onEdit = (row) => {
  editRef.value.open(row)
}

</script>

<template>
  <div class="app">
    <el-table :data="list">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name" width="150"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{row}">
          <el-button type="primary" @click="onEdit(row)" link>编辑</el-button>
          <el-button @click="onDelete(row.id)" type="danger" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- 两种方式子类调用父类的方法:
        1. @X-XX 一般用横杠 子类使用 defineEmit 来接受父类的事件方法
        2. :XXX  一般用驼峰命名 子类使用 defineProps 来接受父类的传递的值或属性
   -->
<!--  <Edit @on-update="getList" ref="editRef"/>-->
  <Edit :onUpdate="getList" ref="editRef"/>
</template>

<style scoped>
.app {
  width: 980px;
  margin: 100px auto 0;
}
</style>
